package com.hnu.seckill.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hnu.seckill.entity.TUser;
import com.hnu.seckill.vo.RespBean;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 生成用户工具类
 */
public class UserUtil {
    public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
        createuser(5000);
    }

    static private void createuser(int count) throws SQLException, ClassNotFoundException, IOException {
        List<TUser> users = new ArrayList<TUser>();
        for (int i = 0; i < count; i++) {
            TUser user = new TUser();
            user.setId(13000000000L + i);
            user.setNickname("user" + i);
            user.setSalt("1a2b3c4d");
            user.setPassword(MD5Util.inputPassToDBPass("123456", user.getSalt()));
            user.setLoginCount(0);
            users.add(user);

        }


//        System.out.println("create user");
//        Connection connection = getConnect();
//        String sql = "insert into t_user(nickname,password," +
//                "salt,head,register_date,last_login_date,login_count,id) " +
//                "values(?,?,?,?,?,?,?,?)";
//        PreparedStatement ps = connection.prepareStatement(sql);
//        for (int i = 0; i < users.size(); i++) {
//            TUser user = users.get(i);
//
//            ps.setString(1, user.getNickname());
//            ps.setString(2, user.getPassword());
//            ps.setString(3, user.getSalt());
//            ps.setString(4, user.getHead());
//            ps.setTimestamp(5, null);
//            ps.setTimestamp(6, null);
//            ps.setInt(7, user.getLoginCount());
//            ps.setLong(8, user.getId());
//            ps.addBatch();
//        }
//        ps.executeBatch();
//        ps.clearParameters();
//        connection.close();
//        System.out.println("insert user");



        String UrlString = "http://localhost:8080/login/doLogin";
        File file = new File("D:\\JavaProject\\SecKill\\UserTickets1.txt");
        if (file.exists()) {
            file.delete();
        }
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        raf.seek(0);
        for (int i = 0; i < users.size(); i++) {
            TUser user = users.get(i);
            URL url = new URL(UrlString);
            HttpURLConnection co = (HttpURLConnection) url.openConnection();
            co.setRequestMethod("POST");
            co.setDoOutput(true);
            OutputStream out = co.getOutputStream();
            String params = "mobile=" + user.getId() + "&password=" + MD5Util.inputPassToFormPass("123456");
            out.write(params.getBytes());
            out.flush();
            InputStream inputStream = co.getInputStream();
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buff)) >= 0) {
                bout.write(buff, 0, len);
            }
            inputStream.close();
            bout.close();
            String response = bout.toString();
            ObjectMapper mapper = new ObjectMapper();
            RespBean respBean = mapper.readValue(response, RespBean.class);
            String userTicket = (String) respBean.getObj();
            System.out.println("create userTicket :" + user.getId());
            String row = user.getId() + "," + userTicket;
            raf.seek(raf.length());
            raf.write(row.getBytes());
            raf.write("\r\n".getBytes());
            System.out.println("write to file :" + user.getId());
        }


        raf.close();
        System.out.println("end");
    }


    private static Connection getConnect() throws ClassNotFoundException, SQLException {
        String url = "jdbc:mysql://192.168.56.10:3306/seckill";
        String username = "root";
        String password = "root";
        String driver = "com.mysql.cj.jdbc.Driver";
        Class.forName(driver);
        return DriverManager.getConnection(url, username, password);
    }
}
